package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.envpro.admin.api.entity.SysLog;
import com.iwomy.envpro.admin.api.feign.RemoteDeptService;
import com.iwomy.envpro.common.security.util.SecurityUtils;
import com.iwomy.envpro.platform.VO.AirRealTimeDataVO;
import com.iwomy.envpro.platform.VO.AlarmLoggingVO;
import com.iwomy.envpro.platform.constant.DeviceStatusConstants;
import com.iwomy.envpro.platform.dto.AlarmLoggingDTO;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.AirRealTimeDataEntity;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.entity.AlarmLoggingEntity;
import com.iwomy.envpro.platform.mapper.AirMonitorEquipmentMapper;
import com.iwomy.envpro.platform.mapper.AlarmLoggingMapper;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.AlarmLoggingService;
import com.iwomy.envpro.platform.utils.DeptFillUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 报警记录
 *
 * @author pig
 * @date 2023-10-05 14:28:40
 */
@Service
public class AlarmLoggingServiceImpl extends ServiceImpl<AlarmLoggingMapper, AlarmLoggingEntity> implements AlarmLoggingService {

	@Resource
	private AirMonitorEquipmentMapper airMonitorEquipmentMapper;
	@Resource
	private RemoteDeptService remoteDeptService;

	@Override
	public Page getAlarmLoggingByPage(Page page, AlarmLoggingDTO alarmLoggingDTO) {


		//多表查询
		MPJLambdaWrapper<AlarmLoggingEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(AlarmLoggingEntity.class)
				.selectAs(AirMonitorEquipmentEntity::getAddress, AlarmLoggingVO::getAddress)
				.leftJoin(AirMonitorEquipmentEntity.class, AirMonitorEquipmentEntity::getId, AlarmLoggingEntity::getEquipmentId)
		;

		wrapper.eq(ObjectUtil.isNotNull(alarmLoggingDTO.getEventStatus()), AlarmLoggingEntity::getEventStatus, alarmLoggingDTO.getEventStatus());

		wrapper.eq(ObjectUtil.isNotNull(alarmLoggingDTO.getMonitorEquipmentId()), AlarmLoggingEntity::getEquipmentId, alarmLoggingDTO.getMonitorEquipmentId());

		if (ArrayUtil.isNotEmpty(alarmLoggingDTO.getDateTime())) {
			wrapper.ge(AlarmLoggingEntity::getCreateTime, alarmLoggingDTO.getDateTime()[0]).le(AlarmLoggingEntity::getCreateTime,
					alarmLoggingDTO.getDateTime()[1]);
		}
		List<Long> data = remoteDeptService.
				listByDeptId(ObjectUtil.isNotNull(alarmLoggingDTO.getDeptId()) ? alarmLoggingDTO.getDeptId() : SecurityUtils.getUser().getDeptId()).getData();
		wrapper.in(ObjectUtil.isNotEmpty(data), AlarmLoggingEntity::getDeptId, data);


		wrapper.in(ObjectUtil.isNotNull(alarmLoggingDTO.getFactorIdList()), AlarmLoggingEntity::getFactorId, alarmLoggingDTO.getFactorIdList());
		page = this.getBaseMapper().selectJoinPage(page, AlarmLoggingVO.class, wrapper);
		DeptFillUtils.fill(page);
		return page;
	}

	@Override
	public boolean updateAlarmLogging(AlarmLoggingEntity alarmLoggingEntity) {

		this.updateAlarmStatus(alarmLoggingEntity);


		return this.updateById(alarmLoggingEntity);
	}

	private void updateAlarmStatus(AlarmLoggingEntity alarmLogging) {
		this.updateById(alarmLogging);
		AlarmLoggingEntity alarmLoggingEntity = this.getById(alarmLogging.getId());
		LambdaQueryWrapper<AlarmLoggingEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(AlarmLoggingEntity::getEquipmentId, alarmLoggingEntity.getEquipmentId()).eq(AlarmLoggingEntity::getEventStatus, 10);

		long count = this.count(wrapper);
		if (count > 0) return;
		AirMonitorEquipmentEntity airMonitorEquipmentEntity = new AirMonitorEquipmentEntity();
		airMonitorEquipmentEntity.setId(alarmLoggingEntity.getEquipmentId());
		airMonitorEquipmentEntity.setAlarmStatus(count > 0 ? DeviceStatusConstants.ABNORMAL : DeviceStatusConstants.NORMAL);
		airMonitorEquipmentMapper.updateById(airMonitorEquipmentEntity);
	}

	@Override
	public AlarmLoggingVO getDetails(Serializable id) {
		//多表查询
		MPJLambdaWrapper<AlarmLoggingEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.selectAll(AlarmLoggingEntity.class)
				.selectAs(AirMonitorEquipmentEntity::getAddress, AlarmLoggingVO::getAddress)
				.selectAs(AirSiteEntity::getName, AlarmLoggingVO::getSiteName)
				.leftJoin(AirMonitorEquipmentEntity.class, AirMonitorEquipmentEntity::getId, AlarmLoggingEntity::getEquipmentId)
				.leftJoin(AirSiteEntity.class, AirSiteEntity::getId, AlarmLoggingEntity::getSiteId)
				.eq(AlarmLoggingEntity::getId, id);
		return this.getBaseMapper().selectJoinOne(AlarmLoggingVO.class, wrapper);
	}
}